library(stargazer)

source("sccbs_recodes.R")
source("cces_recodes.R")

# allows stargazer to use output from clmm models 
# (see https://github.com/andrewheiss/civil-society-authoritarian-survival)

fake.clm <- function(x) {
  model.fake <- x
  model.fake$nobs <- x$dims$nobs
  model.fake$call[1] <- call("clm")
  model.fake
}


mobility.model.pthreat <- glmer(exit ~ fb_d*fb_lag + age + education + tenure + ideology + income + gender + own + kids + black + white + pctunemp + pctblack + medhsvl + (1|community), subset = citizen == 1, family = binomial(link = "logit"), nAGQ=0, control=glmerControl(optimizer = "nloptwrap"))

expression.model.pthreat <- clmm(factor(immig) ~ fb_d*fb_lag + age + education + tenure + ideology + income + gender + own + kids + black + white + pctunemp + pctblack + medhsvl + (1|community), subset = citizen == 1, link = "logit", nAGQ=0, control=glmerControl(optimizer = "nloptwrap"))

stargazer(mobility.model.pthreat, fake.clm(expression.model.pthreat), covariate.labels = c("Delta Immigrant", "Surrounding Composition", "Age", "Education", "Tenure", "Ideology", "Income", "Gender", "Homeowner", "Parent", "Black", "White", "Pct. Unemployed", "Pct. Black", "Median Housing Value", "Delta Immigrant x Surrounding Composition", "Intercept"), no.space = TRUE, single.row = TRUE, digits = 2, star.cutoffs = c(.10, .02, .002))

imm.model.pthreat <- lmer(imm.scale ~ fb_lag*fb_d + gender + age + educ + faminc + pid7 + ideology + gender + own + black + white + lived + pctunemp_01 + pctblack_01 + medianhsvl_01 + (1|lookupzip), data = df.m, subset = imm == 1)

trump.model.pthreat <- glmer(trump ~ fb_lag*fb_d + gender + age + educ + faminc + pid7 + ideology + gender + own + black + white + lived + pctunemp_01 + pctblack_01 + medianhsvl_01 + (1|lookupzip), data = df.m, subset = imm == 1, family = binomial(link="logit"), nAGQ = 0)

stargazer(imm.model.pthreat, trump.model.pthreat, covariate.labels = c("Delta Immigrant", "Surrounding Composition", "Age", "Education", "Tenure", "Ideology", "Income", "Gender", "Homeowner", "Parent", "Black", "White", "Pct. Unemployed", "Pct. Black", "Median Housing Value", "Delta Immigrant x Surrounding Composition", "Intercept"), no.space = TRUE, single.row = TRUE, digits = 2, star.cutoffs = c(.10, .02, .002))

